package com.sunlight.boot.codegen;


import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.IFileCreate;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.FileType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class MyCodeGen extends AutoGenerator {
    public MyCodeGen() {
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setSwagger2(true);
        this.setGlobalConfig(globalConfig);
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, String> packageInfo = this.getConfig().getPackageInfo();
                int i = ((String)packageInfo.get("Entity")).lastIndexOf("entity");
                packageInfo.put("Entity", ((String)packageInfo.get("Entity")).substring(0, i) + "model");
                Map<String, String> pathInfo = this.getConfig().getPathInfo();
                i = ((String)pathInfo.get("entity_path")).lastIndexOf("entity");
                pathInfo.put("entity_path", ((String)pathInfo.get("entity_path")).substring(0, i) + "model");
                Iterator var4 = this.getConfig().getTableInfoList().iterator();

                while(var4.hasNext()) {
                    TableInfo tableInfo = (TableInfo)var4.next();
                    tableInfo.setServiceName(tableInfo.getServiceName().replaceFirst("I", ""));
                }

            }
        };
        cfg.setFileCreate(new IFileCreate() {
            @Override
            public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
                return true;
            }
        });
        this.setCfg(cfg);
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setEntity("codegen/model.java");
        templateConfig.setService("codegen/service.java");
        templateConfig.setController("codegen/controller.java");
        templateConfig.setMapper("codegen/mapper.java");
        templateConfig.setXml("codegen/mapper.xml");
        templateConfig.setServiceImpl("codegen/serviceImpl.java");
        this.setTemplate(templateConfig);
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.no_change);
        strategy.setColumnNaming(NamingStrategy.no_change);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true);
        this.setStrategy(strategy);
    }

    @Override
    protected List<TableInfo> getAllTableInfoList(ConfigBuilder config) {
        List<TableInfo> allTableInfoList = super.getAllTableInfoList(config);
        List<TableInfo> list = new ArrayList();
        Iterator var4 = allTableInfoList.iterator();

        label46:
        while(true) {
            TableInfo tableInfo;
            do {
                if (!var4.hasNext()) {
                    config.setTableInfoList(list);
                    return list;
                }

                tableInfo = (TableInfo)var4.next();
            } while(tableInfo instanceof CustomTableInfo);

            CustomTableInfo customTableInfo = new CustomTableInfo();
            BeanUtils.copyProperties(tableInfo, customTableInfo);
            Iterator var7 = tableInfo.getFields().iterator();

            while(true) {
                while(true) {
                    TableField field;
                    do {
                        if (!var7.hasNext()) {
                            customTableInfo.setConvert(true);
                            list.add(customTableInfo);
                            continue label46;
                        }

                        field = (TableField)var7.next();
                    } while(!field.isKeyFlag());

                    String name = field.getName();
                    customTableInfo.setIdName(name);
                    char[] chars = name.toCharArray();

                    for(int i = 0; i < chars.length; ++i) {
                        char c = chars[i];
                        if (c > 'A' && c < 'Z') {
                            String s = name.substring(0, i).toUpperCase();
                            customTableInfo.setIdPre(s);
                            break;
                        }
                    }
                }
            }
        }
    }
}